//
// perc2 - calculation of the fractal dimension of correlated
// bond percolation cluster hulls
//
// Copyright (C) 2009, 2010 Indrek Mandre <indrek(at)mare.ee>
// http://www.mare.ee/indrek/perc2/, http://code.google.com/p/perc2/
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// 

#include "common.cpp"

static void usage (const char *argv0)
{
  fprintf (stderr, "%s: %s [-r seed] [-R rnd.bin] [-o calccelldist.csv] [-s samplecount] [-g generator]\n", argv0, argv0);
  fprintf (stderr, "    -r seed - set the random seed\n");
  fprintf (stderr, "    -R rnd.bin - random binary bitstream data\n");
  fprintf (stderr, "    -o calccelldist.csv - output data to file\n");
  fprintf (stderr, "    -s samplecount - how many samples to do at first level\n");
  fprintf (stderr, "    -g generator - use the given random generator, put 'list' to see available\n");
  exit(-1);
}

int main (int argc, char *argv[])
{
  big_t SAMPLES = 16384;
  const char *outfn = "calccelldist.csv";
  int rnd_seed = 0;
  const char *rndpath = 0;

  const gsl_rng_type * rng_type = gsl_rng_default;

  for ( int i = 1; i < argc; i++ )
    {
      if ( argv[i][0] != '-' || argv[i][1] == 0 || argv[i][2] != 0 )
          usage (argv[0]);

      switch (argv[i][1])
        {
          case 'o': {
            i++;
            if ( i == argc )
                usage (argv[0]);
            outfn = argv[i];
            break;
          }
          case 'r': {
            i++;
            if ( i == argc || sscanf (argv[i], "%d", &rnd_seed) != 1 )
                usage (argv[0]);
            break;
          }
          case 's': {
            i++;
            if ( i == argc || sscanf (argv[i], "%llu", &SAMPLES) != 1 )
                usage (argv[0]);
            break;
          }
          case 'g': {
            i++;
            if ( i == argc )
                usage (argv[0]);
            rng_type = 0;
            for ( const gsl_rng_type **t = gsl_rng_types_setup(); *t != 0; t++ )
              {
                if ( strcmp ((*t)->name, argv[i]) == 0 )
                  {
                    rng_type = *t;
                    break;
                  }
              }
            if ( !rng_type )
              {
                printf ("Random generators available:\n");
                for ( const gsl_rng_type **t = gsl_rng_types_setup(); *t != 0; t++ )
                    printf ("  %s\n", (*t)->name);
                exit(0);
              }
            break;
          }
          default: {
            usage (argv[0]);
            break;
          }
        }
    }

  rndf.seed (rng_type, rnd_seed, rndpath);

  LAYERSTART = 0;
  LAYEREND = 0;

#define DIST_DIV 200
#define DIST_RANGE 2.0
#define DIST_SIDE ((int)(DIST_DIV*DIST_RANGE))

  size_t count[2 * DIST_SIDE + 1];
  memset (count, 0, sizeof(count));

  long double totcum2 = 0;
  size_t ncum = 0;

  for ( size_t s = 0; s < SAMPLES; s++ )
    {
      double value = v2d(calc_cell());
      int index = round(value * DIST_DIV);
      assert ( index >= -DIST_SIDE && index <= DIST_SIDE );
      ncum++;
      totcum2 += value * value;
      count[DIST_SIDE + index]++;
    }

  FILE *fp = fopen (outfn, "w");
  for ( int i = -DIST_SIDE; i <= DIST_SIDE; i++ )
    {
      if ( !count[i + DIST_SIDE] )
          continue;
      fprintf (fp, "%g %zu\n", (double)i / DIST_DIV, count[i + DIST_SIDE]);
    }
  fclose (fp);
  {
    const char *basename = strrchr (outfn, '/');
    if ( basename )
        basename++;
    else
        basename = outfn;
    std::string newpath = std::string(outfn, outfn + (basename - outfn)) + "var." + basename;
    FILE *fp = fopen (newpath.c_str(), "w");
    fprintf (fp, "1 %zu\n", ncum);
    fprintf (fp, "2 %.12Le\n", totcum2);
    fclose (fp);
  }

  return 0;
}

